package com.wish.datastructure;

import com.wish.inter.datastructure.IBasicArray;

/**
 * Created by suliucheng on 2021/3/30 19:45
 * Directions:环形数组 具有 头 front  尾部 rear  最大容量 maxSize  队列本身 int[] arr
 * front指向队列的头部
 * rear 指向队列尾部的后一位
 * <p>
 * * * * * * * * * maxSize = 8
 * 0 1 2 3 4 5 6 7 8
 * 满时 front 0  rear 8
 * 空时 front 0 rear 0
 */
public class CircleArray implements IBasicArray {

    /**
     * 队列的头
     */
    private int front;
    /**
     * 队列的尾
     */
    private int rear;

    /**
     * 最大值
     */
    private int maxSize;

    /**
     * 队列本身 数组表达
     */

    private int[] array;

    /**
     * 构造器注入
     *
     * @param maxSize 容量初始值
     */
    public CircleArray(int maxSize) {
        this.front = this.rear = 0;
        this.maxSize = maxSize;
        this.array = new int[maxSize];
    }

    /**
     * @return
     */
    @Override
    public boolean isFull() {
        return (rear+1) % maxSize == front;
    }

    /**
     * 初始化的时候 front和 rear同时指向 0 位内存 表示为空队列
     *
     * @return
     */
    @Override
    public boolean isEmpty() {
        return rear == front;
    }

    @Override
    public void pop() {
        if (isEmpty()) {
            System.out.println("队列为空不能够再取出元素");
            return;
        }
        array[front%maxSize] = 0;
        front = front % maxSize + 1;

    }

    @Override
    public void pop(int num) {
        for (int i = 0; i < num; i++) {
            pop();
        }
    }

    @Override
    public void push() {
        if (isFull()) {
            System.out.println("队列此时已经满了，不能够加入元素");
            return;
        }
        array[rear%maxSize] = rear + 1;
        rear = rear % maxSize + 1;
    }

    @Override
    public void push(int num) {
        for (int i = 0; i < num; i++) {
            push();
        }
    }

    @Override
    public void show() {

        for (int i = 0; i < maxSize; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
}
